package one.bugu.android.demon.util;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import com.juefeng.android.framework.common.util.PermissionManager;
import one.bugu.android.demon.ui.activity.WebActivity;

import java.io.*;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: LangK
 * Created Date 2018/1/24
 * Time: 11:17
 * Description:
 */
public class ImageUtils {

    private static final int IMAGE_SIZE_DEFAULT = 500;

    /**
     * 保存图片并且压缩
     *
     * @param photo
     * @param path
     */
    public static void saveImageAndCompress(Bitmap photo, String path) {
        File file = new File(path);
        FileOutputStream out = null;
        try {
            photo = compressBitmap(photo, IMAGE_SIZE_DEFAULT);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            if (!file.exists()) {
                file.createNewFile();
            }
            out = new FileOutputStream(file);
            photo.compress(Bitmap.CompressFormat.PNG, 100, out);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            if (out != null) {
                out.flush();
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 压缩图片
     *
     * @param file
     * @param path
     */
    public static void saveImageAndCompress(File file, String path) {
        File resultFile = new File(path);
        FileOutputStream out = null;
        try {
            Bitmap photo = BitmapFactory.decodeFile(file.getAbsolutePath());
            photo = compressBitmap(photo, IMAGE_SIZE_DEFAULT);
            if (!resultFile.getParentFile().exists()) {
                resultFile.getParentFile().mkdirs();
            }
            if (!resultFile.exists()) {
                resultFile.createNewFile();
            }
            out = new FileOutputStream(resultFile);
            photo.compress(Bitmap.CompressFormat.JPEG, 100, out);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            if (out != null) {
                out.flush();
                out.close();
            }
            file.delete();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * compress bitmap
     *
     * @param bitmap
     * @param sizeLimit
     * @return
     */
    public static Bitmap compressBitmap(Bitmap bitmap, long sizeLimit) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int quality = 100;
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
        // loop
        while (baos.toByteArray().length / 1024 > sizeLimit) {
            // clean baos
            baos.reset();
            bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
            quality -= 10;
        }
        Bitmap newBitmap = BitmapFactory.decodeStream(new ByteArrayInputStream(baos.toByteArray()), null, null);
        return newBitmap;
    }


    private void saveBitmap(Bitmap bitmap, String fileName) {
        try {
            File file = new File(fileName);
            FileOutputStream fos = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 80, fos);
            fos.flush();
            fos.close();
        } catch (java.io.IOException e) {
            e.printStackTrace();
        }
    }


    public static void notifyCarme(Context context, String fileName) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!PermissionManager.checkFilePermission((Activity) context)) {
                return;
            }
        }
        ContentValues values = new ContentValues();
        values.put("datetaken", new Date().toString());
        values.put("mime_type", "image/png");
        values.put("_data", fileName);
        ContentResolver cr = context.getContentResolver();
        cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    }

}
